package me.leiusual.sharing.ui.account

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.Register
import me.leiusual.sharing.api.model.SendSMS
import me.leiusual.sharing.repository.UserRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.ui.account.model.ParamsRegister
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * Create by leiusual on 2018/7/16
 */
class AccountRegisterViewModel @Inject constructor(
    accountRegisterViewModelDelegate: AccountRegisterViewModelDelegate,
    private val userRepository: UserRepository
) : ViewModel(), AccountRegisterViewModelDelegate by accountRegisterViewModelDelegate {

  private val _sendSMS = MutableLiveData<String>()
  val sendSMS: LiveData<Data<Unit>> = Transformations.switchMap(_sendSMS) {
    userRepository.sendSMS(SendSMS.Type.USER_REGISTER, it).toLiveData()
  }

  fun sendSMS(phone: String) {
    _sendSMS.value = phone
  }

  private val _register = MutableLiveData<ParamsRegister>()
  val register: LiveData<Data<Register.Response>> = Transformations.switchMap(_register) {
    userRepository.register(it).toLiveData()
  }

  fun register(mobile: String, checkCode: String, inviteCode: String) {
    _register.value = ParamsRegister(mobile, checkCode, inviteCode)
  }
}